<?php
/**
* Mapping for a collection of related models
* 
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* 
* @copyright Copyright (c) 2008-2009 Ne8, LLC <josh.ribakoff@gmail.com>
* @license New BSD License
* @version 0.1
* @category Shuffler
* @package Shuffler_Mapper
* @subpackage Mapping
*/
class Shuffler_Mapper_Mapping_Collection extends Shuffler_Mapper_Mapping
{
    /**
    * @var bool wether or not this mapping is lazy loading
    */
    protected $lazy_load = true;
    
    /**
    * Setter strategy for Shuffler_Mapper->doLoad()
    * 
    * @param Shuffler_Model to inject data into
    * @return Shuffler_Model after injecting data
    */
    protected function doLoad( Shuffler_Model $model, array $rs )
    {
        $field = $this->getField();

        if( false === $this->getLazyLoad() )
        {
            $collection = $this->loadCollection( $model );
        }
        else
        {        
            $collection = new Shuffler_Collection_Lazy( $this, $model );
        }
        $command = $this->getMutatorMethod();
        if( !method_exists( $model, $command ) )
        {
            throw new Exception( 'Method does not exist ' . get_class( $model ) . '->' . $command );
        }    
        $model->$command( $collection ); 
        return $model; 
    }
    
    /**
    * Load a collection
    * 
    * @param Shuffler_Model foreign model used for constraining
    */
    public function loadCollection( Shuffler_Model $model )
    {
        return $this->getTargetMapper()->findByField( $this->getField(), $model->getId() );
    }
    
    /**
    * Load a collection
    * 
    * @param Shuffler_Model foreign model used for constraining
    */
    public function countCollection( Shuffler_Model $model )
    {
        return $this->getTargetMapper()->countByField( $this->getField(), $model->getId() );
    }
    
    public function select( $select ) 
    {
        return $select;
    }
    
    public function save( Shuffler_Model $model )
    {
        $method = $this->getAccessorMetod();
        $collection = $model->$method();
        foreach( $collection as $model )
        {       
            $this->getMapper()->save( $model );
        }
    }
    
    /**
    * Get the name of the mutator ( adder )
    * 
    * @return string name of method for use in reflective calls
    * 
    * @todo pull up into mapping class & sibling classes
    */
    public function getMutatorMethod()
    {
        return 'set' . $this->getCommandSuffix();
    }
    
    public function getAccessorMetod()
    {
        return 'get' . $this->getCommandSuffix();
    }
    
    public function getType()
    {
        return 'collection';   
    }
    
    /***
    * Test only mehtod
    */
    public function setTargetMapper( $mapper )
    {
        $this->targetMapper = $mapper;
    }
    
    /**
    * Get the class property name for this mapping
    * 
    * used to derive name of setters, getters, adders, etc..
    * 
    * @return string name of property for class
    */
    public function getProperty()
    {
        if( $this->hasPropertyOverride() )
        {
            return $this->property;
        }
        return $this->inflector->getPropertyFromClass( $this->class ) . 's';
    }

}